package com.smart.thirteen.call;

import com.smart.thirteen.Topic;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

/**
 * @author junrui.wang
 * @create 2019/10/21
 */
public class CallTest {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath*:com/smart/thirteen/applicationContext-template.xml");
        JdbcTemplate jdbcTemplate = (JdbcTemplate) ac.getBean("jdbcTemplate");

        final String userID = "222";

        //使用callString
        String callString = "{call P_GET_TOPIC_NUM(?, ?)}";
        Integer num = jdbcTemplate.execute(callString, new CallableStatementCallback<Integer>() {
            @Override
            public Integer doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException {
                cs.setString(1, userID);
                //注册输出参数
                cs.registerOutParameter(2, Types.INTEGER);
                cs.execute();
                //获取输出值
                return cs.getInt(2);
            }
        });

        System.out.println(num);

    }
}
